package com.lagou.edu.service;

import com.lagou.dubbo.EmailDubbo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
public class EmailDubboImpl implements EmailDubbo {

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public boolean sendMail(String email, String code){
        // 发送验证码到邮箱，true成功，false失败
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("xieguannan@gome.com.cn");
            helper.setSubject(code);
            helper.setText(code, true);
            helper.setTo(email);
            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
